<template>
  <s-layout title="确认订单">

    <!--    @tap=""-->
    <!--    &lt;!&ndash; 选择优惠券弹框 &ndash;&gt;-->

    <view class="confirm-order">
      <view class="confirm-order-t">
        <view class="info ss-flex">
          <view class="info-l ">
            <image src="@/static/img/car.png"></image>
          </view>
          <view class="info-r ss-flex-1 ">
            <view class="text-1">红旗E001</view>
            <view class="text-2">绿色</view>
            <view class="text-3">￥89888</view>
          </view>
        </view>
        <view class="remark">
          <view class="remark-title">订单备注</view>
          <view class="remark-content">
            <uni-easyinput
              :inputBorder="false"
              type="textarea"
              maxlength="120"
              autoHeight
              v-model="message"
              placeholder="选填，建议留言前与商家联系  "
            ></uni-easyinput>
          </view>
        </view>
      </view>

      <view class="price">
        <view class="price-t">
          <view class="title">价格明细</view>
          <view class="ss-flex ss-col-center ss-row-between">
            <view class="text-1">
              <view>商品金额</view>
              <view>配送方式</view>
            </view>
            <view class="text-2">
              <view>¥89888.00</view>
              <view>服务中心自提</view>
            </view>
          </view>
        </view>
        <view class="price-b ss-flex ss-col-center ss-row-right">
          <view class="price-b-l ss-flex ss-col-center">
            <view>共1件</view>
            <view>小计：</view>
          </view>
          <view class="price-b-r">￥89888.00</view>
        </view>
      </view>

      <view class="payMethod ss-flex ss-col-center ss-row-between">
        <view class="payMethod-l ss-flex ss-col-center">
          <image class="payMethod-1" src="@/static/img/payMethod-1.png"></image>
          <view>余额支付</view>
        </view>
        <view class="payMethod-r"></view>
      </view>
    </view>

    <!-- 底部 -->
    <su-fixed bottom :opacity="false" bg="" placeholder :noFixed="false" :index="200">
      <view class="footer-box border-top ss-flex ss-row-between ss-p-x-20 ss-col-center">
        <view class="total-box-footer ss-flex ss-col-center">
          <view class="total-num ss-flex">
            <view>共1件</view>
            <view>合计：</view>
          </view>
          <view class="total-price"> ￥{{ state.orderInfo.pay_fee }} 1000</view>
        </view>
        <button
          class=" submit-btn"
          @tap="state.showCoupon = true"
        >
          {{ exchangeNow ? '立即兑换' : '提交订单' }}
        </button>
      </view>
    </su-fixed>

    <PopupConfirm
      v-model="state.couponInfo"
      :show="state.showCoupon"
      @confirm="onConfirm"
      @close="state.showCoupon = false"
    />
  </s-layout>
</template>

<script setup>
  import { computed, reactive, ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import PopupConfirm from './components/popup-confirm/index.vue';

  import sheep from '@/sheep';
  import { isEmpty } from 'lodash';

  const state = reactive({
    orderPayload: {},
    orderInfo: {},
    addressInfo: {},
    invoiceInfo: {},
    totalNumber: 0,
    showCoupon: false,
    couponInfo: [],
    showDiscount: false,
  });
  const message = ref(null);
  // 立即兑换(立即兑换无需跳转收银台)
  const exchangeNow = computed(
    () => state.orderPayload.order_type === 'score' && state.orderInfo.pay_fee == 0,
  );

  // 选择地址
  function onSelectAddress() {
    uni.$once('SELECT_ADDRESS', (e) => {
      changeConsignee(e.addressInfo);
    });
    sheep.$router.go('/pages/user/address/list');
  }

  // 更改收货人地址&计算订单信息
  async function changeConsignee(addressInfo = {}) {
    if (isEmpty(addressInfo)) {
      const { code, data } = await sheep.$api.user.address.default();
      if (code === 1 && !isEmpty(data)) {
        addressInfo = data;
      }
    }
    if (!isEmpty(addressInfo)) {
      state.addressInfo = addressInfo;
      state.orderPayload.address_id = state.addressInfo.id;
    }
    getOrderInfo();
  }

  // 选择优惠券
  async function onSelectCoupon(e) {
    state.orderPayload.coupon_id = e || 0;
    getOrderInfo();
    state.showCoupon = false;
  }

  // 选择发票信息
  function onSelectInvoice() {
    uni.$once('SELECT_INVOICE', (e) => {
      state.invoiceInfo = e.invoiceInfo;
      state.orderPayload.invoice_id = e.invoiceInfo.id || 0;
    });
    sheep.$router.go('/pages/user/invoice/list');
  }

  // 提交订单/立即兑换
  function onConfirm() {
    sheep.$router.go('/pages/inherit/successful');
    // if (!state.orderPayload.address_id && state.orderInfo.need_address === 1) {
    //   sheep.$helper.toast('请选择收货地址');
    //   return;
    // }
    //
    // if (exchangeNow.value) {
    //   uni.showModal({
    //     title: '提示',
    //     content: '确定使用积分立即兑换?',
    //     cancelText: '再想想',
    //     success: async function(res) {
    //       if (res.confirm) {
    //         submitOrder();
    //       }
    //     },
    //   });
    // } else {
    //   submitOrder();
    // }
  }

  // 创建订单&跳转
  async function submitOrder() {
    const { code, data } = await sheep.$api.order.create(state.orderPayload);
    if (code === 1) {
      // 更新购物车列表
      if (state.orderPayload.from === 'cart') {
        sheep.$store('cart').getList();
      }
      if (data.status === 'paid') {
        sheep.$router.redirect('/pages/pay/result', {
          orderSN: data.order_sn,
        });
      } else {
        sheep.$router.redirect('/pages/pay/index', {
          orderSN: data.order_sn,
        });
      }
    }
  }

  // 检查库存&计算订单价格
  async function getOrderInfo() {
    let { code, data } = await sheep.$api.order.calc(state.orderPayload);
    if (code === 1) {
      state.totalNumber = 0;
      state.orderInfo = data;
      state.orderInfo.goods_list.forEach((item) => {
        state.totalNumber += item.goods_num;
      });
    }
  }

  // 获取可用优惠券
  async function getCoupons() {
    const { code, data } = await sheep.$api.order.coupons(state.orderPayload);
    if (code === 1) {
      state.couponInfo = data;
    }
  }

  onLoad(async (options) => {
    if (options.data) {
      state.orderPayload = JSON.parse(options.data);
      changeConsignee();
      if (state.orderPayload.order_type !== 'score') {
        getCoupons();
      }
    }
  });
</script>

<style lang="scss" scoped>
  .confirm-order {
    padding: 15px;
    box-sizing: border-box;

    .confirm-order-t {
      background-color: #1e1e1e;
      border-radius: 10px;

      .info {
        padding: 16px;
        box-sizing: border-box;
        border-bottom: 1px dashed #353535;

        .info-l {
          image {
            width: 70px;
            height: 70px;
            margin-right: 16px;
          }
        }

        .info-r {
          .text-1 {
            font-weight: 400;
            font-size: 16px;
            color: #FFFFFF;
          }

          .text-2 {
            font-weight: 300;
            font-size: 12px;
            color: #767676;
            margin: 7px 0 3px;
          }

          .text-3 {
            font-weight: 500;
            font-size: 18px;
            color: #00A1FF;
          }
        }
      }

      .remark {
        padding: 16px;
        box-sizing: border-box;

        .remark-title {
          font-weight: 400;
          font-size: 16px;
          color: #FFFFFF;
          margin-bottom: 12px;
        }

        .remark-content {
          padding: 14px;
          box-sizing: border-box;
          height: 100px;
          background: #151515;
          border-radius: 10px;
          color: #fff;
        }
      }
    }

    .price {
      background-color: #1e1e1e;
      border-radius: 10px;
      padding: 18px 16px 0;
      box-sizing: border-box;
      margin: 11px 0;

      .price-t {
        border-bottom: 1px solid #353535;

        .title {
          font-weight: 400;
          font-size: 16px;
          color: #FFFFFF;
          margin-bottom: 24px;
        }

        .text-1 {
          font-weight: 300;
          font-size: 15px;
          color: #E4E4E4;

          view {
            margin-bottom: 15px;
          }
        }

        .text-2 {
          font-weight: 300;
          font-size: 15px;
          color: #BABABA;

          view {
            margin-bottom: 20px;
          }
        }
      }

      .price-b {
        padding: 17px 0;

        .price-b-l {
          view {
            font-weight: 300;
            font-size: 15px;
            color: #E4E4E4;
            margin-left: 15px;
          }
        }

        .price-b-r {
          font-weight: 500;
          font-size: 15px;
          color: #00A1FF;
          margin-left: 8px;
        }
      }

    }


    .payMethod {
      background-color: #1e1e1e;
      border-radius: 10px;
      padding: 19px 16px;
      box-sizing: border-box;

      .payMethod-l {
        .payMethod-1 {
          width: 15px;
          height: 15px;
          margin-right: 10px;
        }

        font-weight: 400;
        font-size: 15px;
        color: #FFFFFF;
      }

      .payMethod-r {
      }
    }
  }

  .footer-box {
    padding: 20px 15px;
    box-sizing: border-box;
    background-color: #1e1e1e;

    .total-num {
      font-weight: 300;
      font-size: 15px;
      color: #E4E4E4;

      view {
        margin-right: 10px;
      }
    }

    .total-price {
      font-weight: 500;
      font-size: 15px;
      color: #00A1FF;
    }

    .submit-btn {
      width: 120px;
      height: 50px;
      background: linear-gradient(90deg, #45C9F0, #36F6AF);
      border-radius: 25px;
      margin: 0;
      padding: 0;
    }
  }
</style>
